﻿using System;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using TimeTravel.Components;
using TimeTravel.Extentions;
using TimeTravel.IData;

namespace TimeTravel.SqlServer  
{
    public class SiteMessageData : ISiteMessageData
    {
        #region Fields

        private static readonly SqlHelper DbHelper = new SqlHelper();
        private static readonly string[] Columns = { "[id]", "[title]", "[body]", "[username]","[usermail]","[date]" };

        #endregion

        #region Implementation of IDataBase<SiteMessageInfo>

        /// <summary>
        /// 添加记录
        /// </summary>
        public int Add(SiteMessageInfo message)
        {
            if (message == null) return 0;
            var strSql = new StringBuilder();
            strSql.Append("insert into SiteMessage(");
            strSql.Append("[title],[body],[username],[usermail],[date])");
            strSql.Append(" values ");
            strSql.Append("(@title,@body,@username,@usermail,@date) ;");
            strSql.Append("select @@IDENTITY;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@title", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@body", SqlDbType.NVarChar, 500),
                                            new SqlParameter("@username", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@usermail", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@date", SqlDbType.Int)
                                        };
            parameters[0].Value = message.Title;
            parameters[1].Value = message.Body;
            parameters[2].Value = message.NickName;
            parameters[3].Value = message.Email;
            parameters[4].Value = message.Date;
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            if (id == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update SiteMessage set DeleteFlag = 1 ");
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新记录（未实现）
        /// </summary>
        /// <param name="message"></param>
        public void Update(SiteMessageInfo message)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IList<SiteMessageInfo> GetAll()
        {
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from SiteMessage ", String.Join(",", Columns));
            strSql.Append("where DeleteFlag = 0 ;");
            var result = DbHelper.ExecuteObjectList(strSql.ToString(), DataReaderParser);
            return result.ToList();
        }

        /// <summary>
        /// 根据条件获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RecordSet<SiteMessageInfo> GetBySearch(PageQueryInfo query)
        {
            if(query == null) return new RecordSet<SiteMessageInfo>();
            return DbHelper.ExecutePaging("SiteMessage", query, DataReaderParser, Columns);
        }

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SiteMessageInfo GetOne(int id)
        {
            if (id == 0) return null;
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from SiteMessage ", String.Join(",", Columns));
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            return DbHelper.ExecuteObject(CommandType.Text, strSql.ToString(), DataReaderParser, parameters);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetTotalCount()
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(id) from SiteMessage ");
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
            return result == null ? 0 : Convert.ToInt32(result);
        }

        #endregion

        #region BuildNavMenuObject

        /// <summary>
        /// 从DataReader中构建对象
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private SiteMessageInfo DataReaderParser(IDataReader reader)
        {
            var message = new SiteMessageInfo
                              {
                                  ID = reader.GetInt32("id"),
                                  Title = reader.GetString("title"),
                                  NickName = reader.GetString("username"),
                                  Body = reader.GetString("body"),
                                  Email = reader.GetString("usermail"),
                                  Date = reader.GetInt32("date")
                              };
            return message;
        }

        #endregion
    }
}

